#include <QColorDialog>
#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    /*信号和槽*/
    connect(ui->open, SIGNAL(clicked()), this, SLOT(openCamara()));
    connect(ui->pic, SIGNAL(clicked()), this, SLOT(takingPictures()));
    connect(ui->closeCam, SIGNAL(clicked()), this, SLOT(closeCamara()));
    connect(&thread, SIGNAL(renderedImage(QImage)), this, SLOT(updatePixmap(QImage)));
}

MainWindow::~MainWindow()
{
    thread.closeCamara();
    delete ui;
}
void MainWindow::updatePixmap(const QImage &image)
{
    ui->label->setPixmap(QPixmap::fromImage(image));
}

void MainWindow::on_quitBtn_clicked()
{
    QColorDialog *dlg = new QColorDialog(this);
        dlg->show();
}

/******************************
********* 打开摄像头 ***********
*******************************/

void MainWindow::openCamara()
{
    thread.openCamara();
}


/*************************
********* 拍照 ***********
**************************/
void MainWindow::takingPictures()
{
    if(thread.frame==NULL)
        return;
     QImage image = QImage((const uchar*)thread.frame->imageData, thread.frame->width, thread.frame->height, QImage::Format_RGB888).rgbSwapped();
     ui->label_2->setPixmap(QPixmap::fromImage(image));  // 将图片显示到label上
}

/*******************************
***关闭摄像头，释放资源，必须释放***
********************************/
void MainWindow::closeCamara()
{
    thread.closeCamara();
}

void MainWindow::on_startButton_clicked()
{
    thread.start();
    ui->startButton->setEnabled(false);
    ui->stopButton->setEnabled(true);
}

void MainWindow::on_stopButton_clicked()
{
    if (thread.isRunning())
    {
        thread.stop();
        ui->startButton->setEnabled(true);
        ui->stopButton->setEnabled(false);
    }
}
